package com.joker.aigc.chat.controller;


import com.joker.aigc.chat.domain.dto.GroupMemberAddDto;
import com.joker.aigc.chat.domain.dto.GroupMemberAdminAddDto;
import com.joker.aigc.chat.domain.dto.GroupMemberAdminRevokeDto;
import com.joker.aigc.chat.domain.dto.GroupMemberDelDto;
import com.joker.aigc.chat.domain.dto.GroupMemberExitDto;
import com.joker.aigc.chat.domain.dto.GroupMemberInvitationDto;
import com.joker.aigc.chat.domain.vo.GroupMemberListVo;
import com.joker.aigc.chat.domain.vo.GroupMemberVo;
import com.joker.aigc.chat.domain.vo.RoomGroupVo;
import com.joker.aigc.chat.service.RoomService;
import com.joker.aigc.domain.entity.R;
import com.joker.aigc.utils.RequestHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@Slf4j
@Validated
@RestController
@RequestMapping("/api/1.0/room")
@Tag(name = "room", description = "关于房间的接口信息")
public class RoomController {


    final RoomService roomService;

    /**
     * 当我们点击一个群的时候，就会调用该方法。然后获取群组中的一些消息
     *
     * @param roomId 房间号
     * @return 一个群的数据
     */
    @GetMapping("/public/group")
    @Operation(summary = "获取一个房间里 群组详情(在线人数，详情)")
    public R<RoomGroupVo> groupDetail(@NotNull(message = "房间id不能为空") Long roomId) {
        Long uid = RequestHolder.get().getUid();
        return R.ok(roomService.groupDetail(uid, roomId));
    }


    // 根据用户，获取该用户加入了什么群
    @GetMapping("/public/group/list")
    @Operation(summary = "获取群组消息")
    public R<List<RoomGroupVo>> queryGroupList() {
        Long uid = RequestHolder.get().getUid();
        return R.ok(roomService.queryGroupList(uid));
    }

    /**
     * 获取 某一个房间下 ，该房间里面成员列表的所有信息
     *
     * @param roomId 房间id
     * @return
     */
    @GetMapping("/public/group/member/page")
    @Operation(summary = "获取群成员列表")
    public R<List<GroupMemberVo>> getMemberPage(Long roomId) {
        return R.ok(roomService.getMemberPage(roomId));
    }

    /**
     * 当用户写出 @ 的时候，就得调用这个方法，返回当前这个群里面所有的用户信息
     *
     * @param roomId
     * @return
     */
    @GetMapping("/group/member/list")
    @Operation(summary = "房间内的所有群成员列表-@专用")
    public R<List<GroupMemberListVo>> getMemberList(@NotNull(message = "房间id不能为空") Long roomId) {
        return R.ok(roomService.getMemberList(roomId));
    }

    /**
     * 移除成员
     *
     * @param request
     * @return
     */
    @DeleteMapping("/group/member")
    @Operation(summary = "移除成员")
    public R<String> delMember(@Valid @RequestBody GroupMemberDelDto request) {
        Long uid = RequestHolder.get().getUid();
        roomService.delMember(uid, request);
        return R.ok("移除成员成功");
    }


    @DeleteMapping("/group/member/exit")
    @Operation(summary = "退出群聊")
    public R<String> exitGroup(@Valid @RequestBody GroupMemberExitDto request) {
        Long uid = RequestHolder.get().getUid();
        roomService.exitGroup(uid, request);
        return R.ok("退出群聊成功");
    }


    @PostMapping("/group")
    @Operation(summary = "新增群组，点击拉群友操作")
    public R<Long> addGroup(@Valid @RequestBody GroupMemberAddDto request) {
        Long uid = RequestHolder.get().getUid();
        return R.ok(roomService.addGroup(uid, request));
    }


    @PostMapping("/group/member")
    @Operation(summary = "邀请好友")
    public R<String> addMember(@Valid @RequestBody GroupMemberInvitationDto request) {
        Long uid = RequestHolder.get().getUid();
        roomService.addMember(uid, request);
        return R.ok("已经发出邀请");
    }

    @PutMapping("/group/admin")
    @Operation(summary = "添加管理员")
    public R<String> addAdmin(@Valid @RequestBody GroupMemberAdminAddDto request) {
        Long uid = RequestHolder.get().getUid();
        roomService.addAdmin(uid, request);
        return R.ok("添加管理员信息已经发生发送");
    }

    @DeleteMapping("/group/admin")
    @Operation(summary = "撤销管理员")
    public R<String> revokeAdmin(@Valid @RequestBody GroupMemberAdminRevokeDto request) {
        Long uid = RequestHolder.get().getUid();
        roomService.revokeAdmin(uid, request);
        return R.ok("撤销管理员成功");
    }


}
